home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / pl4019ax.zip / SYSLOG.PL < prev    next >
Perl Script  |  1991-11-13  |  6KB  |  219 lines

  1. #
  2. # syslog.pl
  3. #
  4. # $Log:    syslog.pl,v $
  5. # Revision 4.0  91/03/20  01:26:24  lwall
  6. # 4.0 baseline.
  7. # Revision 3.0.1.4  90/11/10  01:41:11  lwall
  8. # patch38: syslog.pl was referencing an absolute path
  9. # Revision 3.0.1.3  90/10/15  17:42:18  lwall
  10. # patch29: various portability fixes
  11. # Revision 3.0.1.1  90/08/09  03:57:17  lwall
  12. # patch19: Initial revision
  13. # Revision 1.2  90/06/11  18:45:30  18:45:30  root ()
  14. # - Changed 'warn' to 'mail|warning' in test call (to give example of
  15. #   facility specification, and because 'warn' didn't work on HP-UX).
  16. # - Fixed typo in &openlog ("ncons" should be "cons").
  17. # - Added (package-global) $maskpri, and &setlogmask.
  18. # - In &syslog:
  19. #   - put argument test ahead of &connect (why waste cycles?),
  20. #   - allowed facility to be specified in &syslog's first arg (temporarily
  21. #     overrides any $facility set in &openlog), just as in syslog(3C),
  22. #   - do a return 0 when bit for $numpri not set in log mask (see syslog(3C)),
  23. #   - changed $whoami code to use getlogin, getpwuid($<) and 'syslog'
  24. #     (in that order) when $ident is null,
  25. #   - made PID logging consistent with syslog(3C) and subject to $lo_pid only,
  26. #   - fixed typo in "print CONS" statement ($<facility should be <$facility).
  27. #   - changed \n to \r in print CONS (\r is useful, $message already has a \n).
  28. # - Changed &xlate to return -1 for an unknown name, instead of croaking.
  29. #
  30. # tom christiansen <tchrist@convex.com>
  31. # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  32. # NOTE: openlog now takes three arguments, just like openlog(3)
  33. #
  34. # call syslog() with a string priority and a list of printf() args
  35. # like syslog(3)
  36. #
  37. #  usage: require 'syslog.pl';
  38. #
  39. #  then (put these all in a script to test function)
  40. #        
  41. #
  42. #    do openlog($program,'cons,pid','user');
  43. #    do syslog('info','this is another test');
  44. #    do syslog('mail|warning','this is a better test: %d', time);
  45. #    do closelog();
  46. #    
  47. #    do syslog('debug','this is the last test');
  48. #    do openlog("$program $$",'ndelay','user');
  49. #    do syslog('notice','fooprogram: this is really done');
  50. #
  51. #    $! = 55;
  52. #    do syslog('info','problem was %m'); # %m == $! in syslog(3)
  53.  
  54. package syslog;
  55.  
  56. $host = 'localhost' unless $host;    # set $syslog'host to change
  57.  
  58. require 'syslog.ph';
  59.  
  60. $maskpri = &LOG_UPTO(&LOG_DEBUG);
  61.  
  62. sub main'openlog {
  63.     ($ident, $logopt, $facility) = @_;  # package vars
  64.     $lo_pid = $logopt =~ /\bpid\b/;
  65.     $lo_ndelay = $logopt =~ /\bndelay\b/;
  66.     $lo_cons = $logopt =~ /\bcons\b/;
  67.     $lo_nowait = $logopt =~ /\bnowait\b/;
  68.     &connect if $lo_ndelay;
  69.  
  70. sub main'closelog {
  71.     $facility = $ident = '';
  72.     &disconnect;
  73.  
  74. sub main'setlogmask {
  75.     local($oldmask) = $maskpri;
  76.     $maskpri = shift;
  77.     $oldmask;
  78. }
  79.  
  80. sub main'syslog {
  81.     local($priority) = shift;
  82.     local($mask) = shift;
  83.     local($message, $whoami);
  84.     local(@words, $num, $numpri, $numfac, $sum);
  85.     local($facility) = $facility;    # may need to change temporarily.
  86.  
  87.     die "syslog: expected both priority and mask" unless $mask && $priority;
  88.  
  89.     @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
  90.     undef $numpri;
  91.     undef $numfac;
  92.     foreach (@words) {
  93.     $num = &xlate($_);        # Translate word to number.
  94.     if (/^kern$/ || $num < 0) {
  95.         die "syslog: invalid level/facility: $_\n";
  96.     }
  97.     elsif ($num <= &LOG_PRIMASK) {
  98.         die "syslog: too many levels given: $_\n" if defined($numpri);
  99.         $numpri = $num;
  100.         return 0 unless &LOG_MASK($numpri) & $maskpri;
  101.     }
  102.     else {
  103.         die "syslog: too many facilities given: $_\n" if defined($numfac);
  104.         $facility = $_;
  105.         $numfac = $num;
  106.     }
  107.     }
  108.  
  109.     die "syslog: level must be given\n" unless defined($numpri);
  110.  
  111.     if (!defined($numfac)) {    # Facility not specified in this call.
  112.     $facility = 'user' unless $facility;
  113.     $numfac = &xlate($facility);
  114.     }
  115.  
  116.     &connect unless $connected;
  117.  
  118.     $whoami = $ident;
  119.  
  120.     if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
  121.     $whoami = $1;
  122.     $mask = $2;
  123.     } 
  124.  
  125.     unless ($whoami) {
  126.     ($whoami = getlogin) ||
  127.         ($whoami = getpwuid($<)) ||
  128.         ($whoami = 'syslog');
  129.     }
  130.  
  131.     $whoami .= "[$$]" if $lo_pid;
  132.  
  133.     $mask =~ s/%m/$!/g;
  134.     $mask .= "\n" unless $mask =~ /\n$/;
  135.     $message = sprintf ($mask, @_);
  136.  
  137.     $sum = $numpri + $numfac;
  138.     unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  139.     if ($lo_cons) {
  140.         if ($pid = fork) {
  141.         unless ($lo_nowait) {
  142.             do {$died = wait;} until $died == $pid || $died < 0;
  143.         }
  144.         }
  145.         else {
  146.         open(CONS,">/dev/console");
  147.         print CONS "<$facility.$priority>$whoami: $message\r";
  148.         exit if defined $pid;        # if fork failed, we're parent
  149.         close CONS;
  150.         }
  151.     }
  152.     }
  153. }
  154.  
  155. sub xlate {
  156.     local($name) = @_;
  157.     $name =~ y/a-z/A-Z/;
  158.     $name = "LOG_$name" unless $name =~ /^LOG_/;
  159.     $name = "syslog'$name";
  160.     eval &$name || -1;
  161. }
  162.  
  163. sub connect {
  164.     $pat = 'S n C4 x8';
  165.  
  166.     $af_unix = 1;
  167.     $af_inet = 2;
  168.  
  169.     $stream = 1;
  170.     $datagram = 2;
  171.  
  172.     ($name,$aliases,$proto) = getprotobyname('udp');
  173.     $udp = $proto;
  174.  
  175.     ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
  176.     $syslog = $port;
  177.  
  178.     if (chop($myname = `hostname`)) {
  179.     ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
  180.     die "Can't lookup $myname\n" unless $name;
  181.     @bytes = unpack("C4",$addrs[0]);
  182.     }
  183.     else {
  184.     @bytes = (0,0,0,0);
  185.     }
  186.     $this = pack($pat, $af_inet, 0, @bytes);
  187.  
  188.     if ($host =~ /^\d+\./) {
  189.     @bytes = split(/\./,$host);
  190.     }
  191.     else {
  192.     ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
  193.     die "Can't lookup $host\n" unless $name;
  194.     @bytes = unpack("C4",$addrs[0]);
  195.     }
  196.     $that = pack($pat,$af_inet,$syslog,@bytes);
  197.  
  198.     socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
  199.     bind(SYSLOG,$this) || die "bind: $!\n";
  200.     connect(SYSLOG,$that) || die "connect: $!\n";
  201.  
  202.     local($old) = select(SYSLOG); $| = 1; select($old);
  203.     $connected = 1;
  204. }
  205.  
  206. sub disconnect {
  207.     close SYSLOG;
  208.     $connected = 0;
  209. }
  210.  
  211. 1;
  212.